package cc.lotuscard;

import android.hardware.usb.UsbDeviceConnection;
import android.hardware.usb.UsbEndpoint;

public class LotusCardDriver {

	public final static int RT_ALL = 0x52;
	public final static int RT_NOT_HALT = 0x26;
	public final static int AM_A = 0x60;
	public final static int AM_B = 0x61;

	public static UsbDeviceConnection m_UsbDeviceConnection = null;
	public static UsbEndpoint m_InEndpoint = null;
	public static UsbEndpoint m_OutEndpoint = null;

	/*************************************************************/
	public LotusCardDriver() {
	}

	static {
		System.loadLibrary("LotusCardDriver");
	}

	/**
	 *
	 * @param strDeviceName
	 * @param nVID
	 * @param nPID
	 * @param nUsbDeviceIndex
	 * @param unRecvTimeOut
	 * @param bUseExendReadWrite
	 * @return
	 */
	public native int OpenDevice(String strDeviceName, int nVID, int nPID,
                                 int nUsbDeviceIndex, int unRecvTimeOut, boolean bUseExendReadWrite);

	/**
	 *
	 * @param nDeviceHandle
	 */
	public native void CloseDevice(long nDeviceHandle);

	/**
	 *
	 * @param nDeviceHandle
	 * @param nRequestType
	 * @param tLotusCardParam
	 * @return true
	 */
	public native boolean Request(long nDeviceHandle, int nRequestType,
								  LotusCardParam tLotusCardParam);

	/**
	 * @param nDeviceHandle
	 * @param tLotusCardParam
	 * @return true
	 */
	public native boolean Anticoll(long nDeviceHandle,
								   LotusCardParam tLotusCardParam);

	/**
	 * ѡ��
	 *
	 * @param nDeviceHandle
	 * @param tLotusCardParam
	 * @return true
	 */
	public native boolean Select(long nDeviceHandle,
								 LotusCardParam tLotusCardParam);

	/**
	 * @param nDeviceHandle
	 * @param nAuthMode
	 * @param nSectionIndex
	 * @param tLotusCardParam
	 * @return true
	 */
	public native boolean Authentication(long nDeviceHandle, int nAuthMode,
										 int nSectionIndex, LotusCardParam tLotusCardParam);

	/**
	 * @param nDeviceHandle
	 * @return true
	 */
	public native boolean Halt(long nDeviceHandle);

	/**
	 * @param nDeviceHandle
	 * @param nAddress
	 * @param tLotusCardParam
	 * @return true
	 */
	public native boolean Read(long nDeviceHandle, int nAddress,
							   LotusCardParam tLotusCardParam);

	/**
	 *
	 * @param nDeviceHandle
	 * @param nAddress
	 * @param tLotusCardParam
	 */
	public native boolean Write(long nDeviceHandle, int nAddress,
								LotusCardParam tLotusCardParam);

	/**
	 *
	 * @param nDeviceHandle
	 * @param nAddress
	 * @param nValue
	 *            ֵ
	 */
	public native boolean Increment(long nDeviceHandle, int nAddress, int nValue);

	/**
	 * @param nDeviceHandle
	 * @param nAddress
	 * @param nValue
	 *            ֵ
	 */
	public native boolean Decreament(long nDeviceHandle, int nAddress,
									 int nValue);

	/**
	 *
	 * @param nDeviceHandle
	 * @param nAddress
	 */
	public native boolean Restore(long nDeviceHandle, int nAddress);

	/**
	 *
	 * @param nDeviceHandle
	 * @param nAddress
	 */
	public native boolean Transfer(long nDeviceHandle, int nAddress);

	/**
	 *
	 * @param nDeviceHandle
	 * @param nAuthMode
	 * @param nSectionIndex
	 * @param tLotusCardParam
	 */
	public native boolean LoadKey(long nDeviceHandle, int nAuthMode,
								  int nSectionIndex, LotusCardParam tLotusCardParam);

	/**
	 */
	public native boolean Beep(long nDeviceHandle, int nBeepLen);

	public native boolean SendCpuCommand(long nDeviceHandle, int nTimeOut,
										 LotusCardParam tLotusCardParam);

	public native boolean GetCardNo(long nDeviceHandle, int nRequestType,
									LotusCardParam tLotusCardParam);

	/**
	 */
	public native boolean GetCardNoEx(long nDeviceHandle, int nRequestType,
									  byte ucBeepLen, byte ucUseHalt, byte ucUseLoop,
									  LotusCardParam tLotusCardParam);

	/**
	 */
	public native boolean InitValue(long nDeviceHandle, int nAddress, int nValue);

	/**
	 */
	public native boolean ChangePassword(long nDeviceHandle, int nSectionIndex,
                                         String strPasswordA, String strPasswordB);

	/**
	 */
	public native boolean ResetCpuCard(long nDeviceHandle,
									   LotusCardParam tLotusCardParam);

	/**
	 */
	public native boolean ResetCpuCardNoGetCardNo(long nDeviceHandle,
												  LotusCardParam tLotusCardParam);

	/**
	 */
	public native boolean DeselectCpuCard(long nDeviceHandle,
										  LotusCardParam tLotusCardParam);

	/**
	 */
	public native boolean SendCOSCommand(long nDeviceHandle,
										 LotusCardParam tLotusCardParam);

	/**

	 */

	/**

	 */
	public native String GetBankCardNo(long nDeviceHandle);

	/**

	 */
	public native boolean ReadData(long nDeviceHandle, int nRequestType,
								   int nAddress, byte ucUseParameterPassWord, byte ucBeepLen,
								   byte ucUseHalt, LotusCardParam tLotusCardParam);

	/**

	 */
	public native boolean WriteData(long nDeviceHandle, int nAddress,
									byte ucBeepLen, byte ucUseHalt, LotusCardParam tLotusCardParam);

	/****************************** ���´���ΪIPͨ����ش��� **********************************************/
	/**

	 */
	public native String ReadText(long nDeviceHandle, int nSectionIndex);

	/**

	 */
	public native boolean WriteText(long nDeviceHandle, int nSectionIndex,
									String strTextInfo);

	/**

	 */
	public native boolean ConnectTest(String strServerIp, int nConnectTimeOut);

	/**

	 */
	public native int GetErrorCode(long nDeviceHandle);

	/**

	 */
	public native boolean SetCardType(long nDeviceHandle, char cCardType);

	/**

	 */
	public native boolean FelicaPolling(long nDeviceHandle, int unTimerSlot,
										LotusCardParam tLotusCardParam);

	/**

	 */
	public native String ReadNfcBuffer(long nDeviceHandle);

	/**

	 */
	public native boolean WriteNfcBuffer(long nDeviceHandle, String strNfcBuffer);

	/******************************** ���º���Ϊtype b�������� ***************************/
	/**

	 */
	public native int RequestB(long nDeviceHandle, int nRequestType,
							   LotusCardParam tLotusCardParam);

	/**

	 */
	public native int SelectB(long nDeviceHandle, LotusCardParam tLotusCardParam);

	/**

	 */
	public native int HaltB(long nDeviceHandle);

	/**

	 */
	public native String GetDeviceNo(long nDeviceHandle);

	/**

	 */
	public native String GetTwoGenerationIDCardNo(long nDeviceHandle);

	/**

	 */
	public native boolean SetRfOnOff(long nDeviceHandle, byte ucRfOnOff);

	/**

	 */
	public native boolean GetTwoIdInfo(long nDeviceHandle, TwoIdInfoParam tTwoIdInfo);

	/**

	 */
	public native boolean GetTwoIdInfoByServer(long nDeviceHandle, String strServerIp, TwoIdInfoParam tTwoIdInfo);

	/**

	 */
	public static boolean callBackProcess(long nDeviceHandle, boolean bRead,
										  byte[] arrBuffer) {
		int nResult = 0;
		boolean bResult = false;
		int nBufferLength = arrBuffer.length;
		int nWaitCount = 0;
		if (null == m_UsbDeviceConnection)
			return false;
		if (null == m_OutEndpoint)
			return false;
		if (null == m_InEndpoint)
			return false;
		if (nBufferLength != 64)
			return false;
		if (true == bRead) {
			arrBuffer[0] = 0;
			while (true) {
				nResult = m_UsbDeviceConnection.bulkTransfer(m_InEndpoint,
						arrBuffer, nBufferLength, 3000);
				if (nResult <= 0)
					break;
				if (arrBuffer[0] != 0)
					break;
				nWaitCount++;
				if (nWaitCount > 1000)
					break;
			}
			if (nResult == nBufferLength) {
				bResult = true;
			} else {
				bResult = false;
			}
		} else {
			nResult = m_UsbDeviceConnection.bulkTransfer(m_OutEndpoint,
					arrBuffer, nBufferLength, 3000);
			if (nResult == nBufferLength) {
				bResult = true;
			} else {
				bResult = false;
			}
		}
		return bResult;
	}
}
